﻿using System;
using System.Windows;
using System.Windows.Media;

namespace MovieBuddy.Controls.MSTB
{
    /// <summary>
    /// Interaction logic for MSTBItem.xaml
    /// </summary>
// ReSharper disable InconsistentNaming
    public partial class MSTBItem
// ReSharper restore InconsistentNaming
    {
        public MSTBItem()
        {
            InitializeComponent();
            button1.Click += ButtonClicked;
        }

        void ButtonClicked(object sender, RoutedEventArgs e)
        {
            RemoveButtonClicked(sender, new ItemRemoveButtonEventArgs(this));
        }

        private MultiSelectTextBoxItem item;
        internal MultiSelectTextBoxItem Item
        {
            get { return item; }
            set
            {
                item = value;
                label1.Content = item.Text;
                FixWidth();
            }
        }

        public double FixWidth()
        {
            //Measure it
            Measure(new Size(Double.PositiveInfinity, Double.PositiveInfinity));
            var measureRect = new Rect(label1.DesiredSize);
            Arrange(measureRect);


            double labelWidth = measureRect.Width;
            Width = labelWidth + 20;
            return Width;
        }

        public Brush ItemBackground
        {
            get { return grid.Background; }
            set { grid.Background = value; }
        }

        internal event EventHandler<ItemRemoveButtonEventArgs> RemoveButtonClicked;
    }

    internal class ItemRemoveButtonEventArgs : EventArgs
    {
        internal MSTBItem Item { get; private set; }

        internal ItemRemoveButtonEventArgs(MSTBItem item)
        {
            Item = item;
        }
    }
}
